/*=============================================================================

  Library: CFS

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include "CFSVersion.h"

#include "CFSException.h"

#include <QStringListIterator>
#include <QDebug>


const QString CFSVersion::SEPARATOR = ".";
const QRegExp CFSVersion::RegExp = QRegExp("[a-zA-Z0-9_\\-]*");

//----------------------------------------------------------------------------
CFSVersion CFSVersion::emptyVersion()
{
  static CFSVersion emptyV(false);
  return emptyV;
}

//----------------------------------------------------------------------------
CFSVersion CFSVersion::undefinedVersion()
{
  static CFSVersion undefinedV(true);
  return undefinedV;
}

//----------------------------------------------------------------------------
CFSVersion& CFSVersion::operator=(const CFSVersion& v)
{
  majorVersion = v.majorVersion;
  minorVersion = v.minorVersion;
  microVersion = v.microVersion;
  qualifier = v.qualifier;
  undefined = v.undefined;
  return *this;
}

//----------------------------------------------------------------------------
CFSVersion::CFSVersion(bool undefined)
  : majorVersion(0), minorVersion(0), microVersion(0), qualifier(""), undefined(undefined)
{

}

//----------------------------------------------------------------------------
void CFSVersion::validate()
{
  if (!RegExp.exactMatch(qualifier))
    throw CFSInvalidArgumentException(QString("invalid qualifier: ") + qualifier);

  undefined = false;
}

//----------------------------------------------------------------------------
CFSVersion::CFSVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion)
  : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(""),
    undefined(false)
{

}

//----------------------------------------------------------------------------
CFSVersion::CFSVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion, const QString& qualifier)
   : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(qualifier),
     undefined(true)
{
  this->validate();
}

//----------------------------------------------------------------------------
CFSVersion::CFSVersion(const QString& version)
  : majorVersion(0), minorVersion(0), microVersion(0), undefined(true)
{
  unsigned int maj = 0;
  unsigned int min = 0;
  unsigned int mic = 0;
  QString qual("");

  QStringList st = version.split(SEPARATOR);

  if (st.empty()) return;

  QStringListIterator i(st);

  bool ok = true;
  maj = i.next().toUInt(&ok);

  if (i.hasNext())
  {
    min = i.next().toUInt(&ok);
    if (i.hasNext())
    {
      mic = i.next().toUInt(&ok);
      if (i.hasNext())
      {
        qual = i.next();
        if (i.hasNext())
        {
           ok = false;
        }
      }
    }
  }

  if (!ok) throw CFSInvalidArgumentException("invalid format");

  majorVersion = maj;
  minorVersion = min;
  microVersion = mic;
  qualifier = qual;
  this->validate();
}

//----------------------------------------------------------------------------
CFSVersion::CFSVersion(const CFSVersion& version)
: majorVersion(version.majorVersion), minorVersion(version.minorVersion),
  microVersion(version.microVersion), qualifier(version.qualifier),
  undefined(version.undefined)
{

}

//----------------------------------------------------------------------------
CFSVersion CFSVersion::parseVersion(const QString& version)
{
  if (version.isEmpty())
  {
    return emptyVersion();
  }

  QString version2 = version.trimmed();
  if (version2.isEmpty())
  {
    return emptyVersion();
  }

  return CFSVersion(version2);
}

//----------------------------------------------------------------------------
bool CFSVersion::isUndefined() const
{
  return undefined;
}

//----------------------------------------------------------------------------
unsigned int CFSVersion::getMajor() const
{
  if (undefined) throw CFSIllegalStateException("Version undefined");
  return majorVersion;
}

//----------------------------------------------------------------------------
unsigned int CFSVersion::getMinor() const
{
  if (undefined) throw CFSIllegalStateException("Version undefined");
  return minorVersion;
}

//----------------------------------------------------------------------------
unsigned int CFSVersion::getMicro() const
{
  if (undefined) throw CFSIllegalStateException("Version undefined");
  return microVersion;
}

//----------------------------------------------------------------------------
QString CFSVersion::getQualifier() const
{
  if (undefined) throw CFSIllegalStateException("Version undefined");
  return qualifier;
}

//----------------------------------------------------------------------------
QString CFSVersion::toString() const
{
  if (undefined) return "undefined";

  QString result;
  result += QString::number(majorVersion) + SEPARATOR + QString::number(minorVersion) + SEPARATOR + QString::number(microVersion);
  if (!qualifier.isEmpty())
  {
    result += SEPARATOR + qualifier;
  }
  return result;
}

//----------------------------------------------------------------------------
bool CFSVersion::operator==(const CFSVersion& other) const
{
  if (&other == this)
  { // quicktest
    return true;
  }

  if (other.undefined && this->undefined) return true;
  if (this->undefined) throw CFSIllegalStateException("Version undefined");
  if (other.undefined) return false;

  return (majorVersion == other.majorVersion) && (minorVersion == other.minorVersion) && (microVersion
      == other.microVersion) && qualifier == other.qualifier;
}

//----------------------------------------------------------------------------
int CFSVersion::compare(const CFSVersion& other) const
{
  if (&other == this)
  { // quicktest
    return 0;
  }

  if (this->undefined || other.undefined)
    throw CFSIllegalStateException("Cannot compare undefined version");

  if (majorVersion < other.majorVersion)
  {
    return -1;
  }

  if (majorVersion == other.majorVersion)
  {

    if (minorVersion < other.minorVersion)
    {
      return -1;
    }

    if (minorVersion == other.minorVersion)
    {

      if (microVersion < other.microVersion)
      {
        return -1;
      }

      if (microVersion == other.microVersion)
      {
        return qualifier.compare(other.qualifier);
      }
    }
  }
  return 1;
}

//----------------------------------------------------------------------------
bool CFSVersion::operator <(const CFSVersion &object) const
{
  return this->compare(object) < 0;
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, const CFSVersion& v)
{
  dbg << v.toString();

  return dbg.maybeSpace();
}
